from rest_framework import serializers

from .models import DeviceModel, CaseModel, CaseStepModel, StepResultModel, TestRunModel, SectionModel, ProjectModel, \
    AppiumConfigModel


class AppiumParameterSerializer(serializers.ModelSerializer):
    class Meta:
        model = AppiumConfigModel
        fields = '__all__'


class ProjectSerializer(serializers.ModelSerializer):
    appium_config = AppiumParameterSerializer()

    class Meta:
        model = ProjectModel
        fields = '__all__'


class DeviceSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeviceModel
        fields = ['id', 'udid', 'name', 'imei', 'status']  # 需要返回的字段

    udid = serializers.CharField(required=False, allow_blank=True)
    name = serializers.CharField(required=False, allow_blank=True)
    imei = serializers.CharField(required=False, allow_blank=True)
    status = serializers.CharField(required=False, allow_blank=True)


class StepResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = StepResultModel
        fields = '__all__'


class CaseStepSerializer(serializers.ModelSerializer):
    subSteps = serializers.SerializerMethodField()
    results = StepResultSerializer(many=True)

    class Meta:
        model = CaseStepModel
        fields = '__all__'

    @staticmethod
    def get_subSteps(obj):
        subSteps = obj.subSteps.all().order_by('order')
        return CaseStepSerializer(subSteps, many=True).data


class SectionModelSerializer(serializers.ModelSerializer):
    subSections = serializers.SerializerMethodField()

    class Meta:
        model = SectionModel
        fields = '__all__'

    @staticmethod
    def get_subSections(obj):
        result = obj.subSections.all().order_by('id')
        return SectionModelSerializer(result, many=True).data


class CaseSerializer(serializers.ModelSerializer):
    steps = CaseStepSerializer(many=True, read_only=True)
    section = SectionModelSerializer()

    class Meta:
        model = CaseModel
        fields = '__all__'

    def create(self, validated_data):
        # 处理嵌套序列化数据（如果需要），例如 `steps`
        return super().create(validated_data)

    def update(self, instance, validated_data):
        # 处理更新时的数据，确保正确更新
        return super().update(instance, validated_data)


class TestRunSerializer(serializers.ModelSerializer):
    cases = CaseSerializer(many=True)

    class Meta:
        model = TestRunModel
        fields = ['id', 'cases', 'status', 'started_at']


class TestRunCreateSerializer(serializers.ModelSerializer):
    cases = serializers.PrimaryKeyRelatedField(queryset=CaseModel.objects.all(), many=True)

    class Meta:
        model = TestRunModel
        fields = ['id', 'cases', 'status', 'started_at', 'ends_at', 'name']

    def create(self, validated_data):
        cases = validated_data.pop('cases')
        test_run = TestRunModel.objects.create(**validated_data)
        test_run.cases.set(cases)  # 关联多个用户
        return test_run

# class SectionSerializer(serializers.ModelSerializer):
#     sub_section = serializers.SerializerMethodField()
#
#     class Meta:
#         model = SectionModel
#         fields = '__all__'
#
#     def sub_section(self, obj):
#         result = obj.sub_sections.all().order_by('id')
#         return SectionSerializer(result, many=True).data
